package execute

import (
	"github.com/robertkrimen/otto"
	"io/ioutil"
	"log"
)

var vm *otto.Otto

func ExecJs(funcName string, param []interface{}) {
	vm.Call(funcName, nil, param)
}
func ExecJsScript(funcName string, script string, param []interface{}) {
	scriptVm := otto.New()
	if _, err := scriptVm.Run(script); err == nil {
		log.Println("loan js func script success :" + script)
	} else {
		panic(err)
	}
	scriptVm.Call(funcName, nil, param)
}

func GetScript(funcName string) string {
	file, err := ioutil.ReadFile(funcName)

	if err != nil {
		panic(err)
	}
	return string(file)
}

func Init0(p string) {
	vm = otto.New()
	if _, err := vm.Run(GetScript(p)); err == nil {
		log.Println("loan js func script success")
	} else {
		panic(err)
	}
}
